
// Route sigleton

Berta.Route = {};
Berta.Route.get = function(routeName, url_variables, data, dataWithoutContainer) {
	if(Berta.Routes.hasOwnProperty(routeName)) {
		var route = Berta.Routes[routeName];
		var r = { url: route.url };
		
		// Replace variables in URL
		if(route.variables && route.variables.length > 0 && url_variables) {
			jQuery.each(url_variables, function(k, v) {
				if(route.variables.indexOf(k) >= 0) {
					r.url = r.url.replace('$' + k, v);
				}
			});
		}

		r.fileDataKey = null;
		r.fileUpload = false;

		r.container = route.container;

		// Add Data
		r.data = route.data || {};
		if(data) {
			if(route.container) {
				r.data[route.container] = data;
			} else {
				r.data = data;
			}

			// Look for FileList object among data keys
			var d = route.container ? r.data[route.container] : r.data;
			for(var data_key in d) {
				if(d.hasOwnProperty(data_key) && (d[data_key] instanceof FileList || d[data_key] instanceof File)) {
					r.fileDataKey = data_key;
					r.fileUpload = true;
				}
			}
		}

		// Add remaining data (without container)
		if(dataWithoutContainer) {
			jQuery.each(dataWithoutContainer, function(k, v) {
				r.data[k] = v;
			});
		}
		
		r.name = routeName;
		return r;
	}
	
	return null;
};